package by.dkusch.aircompany.webapp.page.users.panel;

import javax.inject.Inject;
import javax.persistence.metamodel.SingularAttribute;

import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.markup.html.form.AjaxButton;
import org.apache.wicket.extensions.markup.html.repeater.data.sort.OrderByBorder;
import org.apache.wicket.extensions.markup.html.repeater.data.sort.SortOrder;
import org.apache.wicket.extensions.markup.html.repeater.data.table.filter.FilterForm;
import org.apache.wicket.markup.html.WebMarkupContainer;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.form.CheckBox;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.markup.html.navigation.paging.PagingNavigator;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.markup.repeater.Item;
import org.apache.wicket.markup.repeater.data.DataView;
import org.apache.wicket.model.PropertyModel;

import by.dkusch.aircompany.model.filter.FilterUserProfile;
import by.dkusch.aircompany.model.tables.UserProfile;
import by.dkusch.aircompany.model.tables.UserProfile_;
import by.dkusch.aircompany.services.UserService;
import by.dkusch.aircompany.webapp.page.base.BaseLayout;
import by.dkusch.aircompany.webapp.page.base.common.AddNewItemPanel;
import by.dkusch.aircompany.webapp.page.base.common.CommonActionPanel;
import by.dkusch.aircompany.webapp.page.base.common.EntityDataProviderFilter;
import by.dkusch.aircompany.webapp.page.base.common.GrantCommonPanel;
import by.dkusch.aircompany.webapp.page.base.panels.MenuPanel;
import by.dkusch.aircompany.webapp.page.users.UserProfilePage;

public class ProfileListPanel extends Panel {

	private static final long serialVersionUID = 1L;
	@Inject
	private UserService userService;
	private EntityDataProviderFilter<Long, UserProfile, FilterUserProfile> userDataProvider;

	public ProfileListPanel(String id, int itemsPerPage) {
		super(id);
		userDataProvider = new EntityDataProviderFilter<Long, UserProfile, FilterUserProfile>(userService);
		userDataProvider.setSort(UserProfile_.code, SortOrder.ASCENDING);
		userDataProvider.setFilterState(new FilterUserProfile());

		setOutputMarkupId(true);

		DataView<UserProfile> dataView = new DataView<UserProfile>("userList", userDataProvider, itemsPerPage) {
			private static final long serialVersionUID = 1L;

			@Override
			protected void populateItem(Item<UserProfile> item) {
				final UserProfile user = item.getModelObject();

				item.add(new CheckBox("arcFl").setEnabled(false));
				item.add(new CheckBox("dbaFl").setEnabled(false));
				item.add(new Label("userAccount.email"));
				item.add(new Label("code"));
				item.add(new Label("firstName"));
				item.add(new Label("secondName"));
				item.add(new Label("lastName"));
				item.add(new CommonActionPanel<Long, UserProfile, UserProfilePage>("action-panel", userService, user, UserProfilePage.class, ProfileListPanel.this));
				item.add(new GrantCommonPanel<Long, UserProfile, UserProfilePage>("grant-panel", user, UserProfilePage.class));
			}
		};


		FilterForm<FilterUserProfile> filterForm = new FilterForm<FilterUserProfile>("filterForm", userDataProvider);
		filterForm.add(new TextField<String>("filter-code", PropertyModel.<String> of(userDataProvider, "filterState.code")));
		filterForm.add(new TextField<String>("filter-email", PropertyModel.<String> of(userDataProvider, "filterState.email")));
		filterForm.add(new TextField<String>("filter-firstName", PropertyModel.<String> of(userDataProvider, "filterState.firstName")));
		filterForm.add(new TextField<String>("filter-secondName", PropertyModel.<String> of(userDataProvider, "filterState.secondName")));
		filterForm.add(new TextField<String>("filter-lastName", PropertyModel.<String> of(userDataProvider, "filterState.lastName")));

		final WebMarkupContainer divTable = new WebMarkupContainer("div-table");
		final PagingNavigator pagingNavigator = new PagingNavigator("paging", dataView);
		divTable.setOutputMarkupId(true);
		pagingNavigator.setOutputMarkupId(true);

		filterForm.add(new AjaxButton("user-search", filterForm) {
			/**
				 * 
				 */
			private static final long serialVersionUID = 1L;

			@Override
			protected void onSubmit(AjaxRequestTarget target, Form<?> form) {
				// repaint the feedback panel so that it is hidden
				target.add(((MenuPanel) ((BaseLayout) getPage()).getMenuPanel()).getFeedback());
				target.add(divTable);
				target.add(pagingNavigator);
			}

			@Override
			protected void onError(AjaxRequestTarget target, Form<?> form) {
				// repaint the feedback panel so errors are shown
				target.add(((MenuPanel) ((BaseLayout) getPage()).getMenuPanel()).getFeedback());
				target.add(divTable);
			}
		});

		divTable.add(dataView);
		divTable.add(new AddNewItemPanel<Long, UserProfile, UserProfilePage>("new-panel", new UserProfile(), UserProfilePage.class));

		divTable.add(new OrderByBorder<SingularAttribute<UserProfile, ?>>("sortByCode", UserProfile_.code, userDataProvider));
		divTable.add(new OrderByBorder<SingularAttribute<UserProfile, ?>>("sortByFName", UserProfile_.firstName, userDataProvider));
		divTable.add(new OrderByBorder<SingularAttribute<UserProfile, ?>>("sortBySName", UserProfile_.secondName, userDataProvider));
		divTable.add(new OrderByBorder<SingularAttribute<UserProfile, ?>>("sortByLName", UserProfile_.lastName, userDataProvider));
		divTable.add(new OrderByBorder<SingularAttribute<UserProfile, ?>>("sortByEmail", UserProfile_.userAccount, userDataProvider));

		filterForm.add(divTable);
		add(pagingNavigator);
		add(filterForm);
	}

}
